package com.cheer.ba.view.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.comspecailvideoview.TextureVideoView;
import com.comspecailvideoview.UniversalMediaController;
import com.comspecailvideoview.UniversalVideoView;
import com.cheer.ba.R;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.utils.NetUtils;
import com.cheer.ba.utils.ToastUtil;

/**
 * 播放器-全屏
 */
public class FullScreenAty extends AppCompatActivity implements UniversalVideoView.VideoViewCallback {

    //视频播放地址
    private String videoUrl;
    //播放进度记录
    private int mSeekPosition;
    //是否可旋转
    private boolean enableScale = true;
    //视频播放控件
    TextureVideoView mVideoView;
    private UniversalMediaController universalMediaController;
    //跟布局
    View mVideoLayout;

    private static final String SEEK_POSITION_KEY = "SEEK_POSITION_KEY";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_full_screen);
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        videoUrl = getIntent().getStringExtra(IntentKey.General.KEY_URL);
        mSeekPosition = getIntent().getIntExtra(IntentKey.General.KEY_POS, 0);
        enableScale = getIntent().getBooleanExtra(IntentKey.General.KEY_BOOLEAN, true);
        if (TextUtils.isEmpty(videoUrl) && savedInstanceState != null) {
            videoUrl = savedInstanceState.getString(IntentKey.General.KEY_URL);
            mSeekPosition = savedInstanceState.getInt(IntentKey.General.KEY_POS, 0);
            enableScale = savedInstanceState.getBoolean(IntentKey.General.KEY_BOOLEAN, true);
        }
        mVideoLayout = findViewById(R.id.video_layout);
        mVideoView = (TextureVideoView) findViewById(R.id.videoView);
        initVideoView();
        setVideoAreaSize();
        mVideoView.setVideoViewCallback(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("yeying", "FullScreenAty destory");
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        outState.putString(IntentKey.General.KEY_URL, videoUrl);
        outState.putInt(IntentKey.General.KEY_POS, mVideoView.getCurrentPosition());
        outState.putBoolean(IntentKey.General.KEY_BOOLEAN, enableScale);
        super.onSaveInstanceState(outState, outPersistentState);
    }

    private void initVideoView() {
//        mVideoView.setVideoPath(VIDEO_URL);
        universalMediaController = (UniversalMediaController) findViewById(R.id.media_controller);
        if (!enableScale) {
            universalMediaController.setFullscreenEnabled(false);
        }
        mVideoView.setMediaController(universalMediaController);
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(final MediaPlayer mp) {
//                startVideoAnimation();
//                mVideoView.start();
            }
        });
        mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
//                idIvPreview.setVisibility(View.VISIBLE);
                mSeekPosition = mVideoView.getCurrentPosition();
                if (mSeekPosition < 0) {
                    mSeekPosition = 0;
                }
                ToastUtil.showToast("视频播放错误");
                return false;
            }
        });
        mVideoView.setVideoViewCallback(this);
    }

    /**
     * 置视频区域大小
     */
    private void setVideoAreaSize() {
        mVideoLayout.post(new Runnable() {
            @Override
            public void run() {
                Log.d("yeying", "full setVideoAreaSize");
                ViewGroup.LayoutParams layoutParams = mVideoLayout.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                mVideoLayout.setLayoutParams(layoutParams);
                mVideoView.requestFocus();
                if (!NetUtils.isNetworkConnected(FullScreenAty.this)) {
                    ToastUtil.showToast("当前网络不可用,请检查您的网络设置");
                    return;
                }
                mVideoView.start();
                mVideoView.setVideoPath(videoUrl);
                universalMediaController.showLoading();
                mVideoView.seekTo(mSeekPosition);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("yeying", "full onPause");
//        if (mVideoView != null && mVideoView.isPlaying()) {
//            mSeekPosition = mVideoView.getCurrentPosition();
//            mVideoView.pause();
//        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("yeying", "full onResume");
//        if (mVideoView.mSeekWhenPrepared == 0) {
//            mVideoView.seekTo(mSeekPosition);
//        }
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(SEEK_POSITION_KEY, mSeekPosition);
    }

    @Override
    protected void onRestoreInstanceState(Bundle outState) {
        super.onRestoreInstanceState(outState);
        mSeekPosition = outState.getInt(SEEK_POSITION_KEY);
    }


    @Override
    public void onScaleChange(boolean isFullscreen) {
        Intent i = new Intent();
        i.putExtra(IntentKey.General.KEY_POS, mVideoView.getCurrentPosition());
        setResult(RESULT_OK, i);
        finish();
    }

    @Override
    public void onPause(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onStart(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onBufferingStart(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onBufferingEnd(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onBackPressed() {
        Intent i = new Intent();
        i.putExtra(IntentKey.General.KEY_POS, mVideoView.getCurrentPosition());
        setResult(RESULT_OK, i);
        super.onBackPressed();
    }
}
